﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Vlc.DotNet.Core;
using Vlc.DotNet.Core.Medias;
using Vlc.DotNet.Forms;


namespace mimamoru
{

    public partial class Form1 : Form
    {
        public Vlc.DotNet.Forms.VlcControl vlcControl;
        public Vlc.DotNet.Core.Medias.PathMedia Media2Play;
        private static bool _isRunning = false;


        public Form1()

        {
            InitializeComponent();


            vlcControl = new Vlc.DotNet.Forms.VlcControl();
            vlcControl.CreateControl();
            vlcControl.Show();

           // this panel requires to catche double click evetns.
            Panel panelDoubleClick = new Panel();
            panelDoubleClick.Dock = DockStyle.Fill;
            panelDoubleClick.BackColor = Color.Transparent;
            panelDoubleClick.MouseDoubleClick += new MouseEventHandler(panelDoubleClick_MouseDoubleClick); ;
            panelVideo.MouseWheel += new System.Windows.Forms.MouseEventHandler(panelVideo_MouseWheelHandler);


            vlcControl.Dock = DockStyle.Fill;
            this.panelVideo.Controls.Add(vlcControl);//panelVideo is manin container panel.
            //initEvents();//VLC player events
            //4paud+play


            vlcControl.Controls.Add(panelDoubleClick);
            panelDoubleClick.BringToFront();

        }

        public void Form1_Load(object sender, EventArgs e)
        {

            if (_isRunning)
                return;

            ControlPanel cpanel = new ControlPanel();
            cpanel.Show();

            OpenFileDialog fdlg = new OpenFileDialog();

            fdlg.Title = "Where is the Animu?";
            fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
            fdlg.Multiselect = true;
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;

            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                //pathtofile = fdlg.FileName;
                this.Text = "Mimomoru - " + fdlg.SafeFileName;

                foreach (String file in fdlg.SafeFileNames)
                {
                    try
                    {

                        //MessageBox.Show(file);
                        cpanel.aniList.Items.Add(file);
                        cpanel.aniLoc.Items.Add(fdlg.FileName);

                    }

                    catch (Exception v)
                    {
                        MessageBox.Show(v.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                cpanel.aniList.SelectedItem = cpanel.aniList.SelectedIndex = 0;
                cpanel.aniLoc.SelectedItem = cpanel.aniLoc.SelectedIndex = 0;

                Media2Play = new Vlc.DotNet.Core.Medias.PathMedia(cpanel.aniLoc.SelectedItem.ToString());

                //MessageBox.Show(vlcControl.Size.Width.ToString());

                vlcControl.Media = Media2Play;    
           
                this.Height =  vlcControl.Size.Height;
                this.Width = vlcControl.Size.Width;
                this.Scale(new SizeF(vlcControl.Size.Height, vlcControl.Size.Width));

                var screen = Screen.AllScreens[0].Bounds;

                this.MaximumSize = new System.Drawing.Size(screen.Width, screen.Height);

                _isRunning = true;
            }

            
}
        bool isFullScreen = false;
                   
        private void panelDoubleClick_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (isFullScreen == false)
            {
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                var screen = Screen.AllScreens[0].Bounds;
               
                this.Width = screen.Width;
                this.Height = screen.Height;
                
                this.Left = screen.X + (screen.Width / 2) - (this.Width / 2);
                this.Top = screen.Y + (screen.Height / 2) - (this.Height / 2);
                isFullScreen = true;
            }

            else if (isFullScreen == true)
            {
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
                this.Width = vlcControl.VideoProperties.Size.Width / 2;
                this.Height = vlcControl.VideoProperties.Size.Height / 2;
               // this.AutoSize = true;
                
                isFullScreen = false;
            }
        }

        private void initVLC()
        {

            try
            {
                // Set libvlc.dll and libvlccore.dll directory path
                string vlcPath = "";

                vlcPath = "E:\\VLC\\VLC_minimal";

                if (System.IO.Directory.Exists(vlcPath) == false)
                {
                    vlcPath = Application.StartupPath.Trim('\\') + "\\VLC\\";
                    if (System.IO.Directory.Exists(vlcPath) == false)
                    {
                        if (Environment.Is64BitOperatingSystem)
                        {
                            vlcPath = "C:\\Program Files (x86)\\VideoLAN\\VLC";
                        }
                        else
                        {
                            vlcPath = "C:\\Program Files\\VideoLAN\\VLC";
                        }
                        if (System.IO.Directory.Exists(vlcPath) == false)
                        {
                            MessageBox.Show("VLC cannot be fount on your system.");
                            Application.Exit();
                            return;
                        }
                    }
                }

                //Set libvlc.dll and libvlccore.dll directory path
                VlcContext.LibVlcDllsPath = CommonStrings.LIBVLC_DLLS_PATH_DEFAULT_VALUE_AMD64;
                //Set the vlc plugins directory path
                VlcContext.LibVlcPluginsPath = CommonStrings.PLUGINS_PATH_DEFAULT_VALUE_AMD64;

                // Ignore the VLC configuration file
                Vlc.DotNet.Core.VlcContext.StartupOptions.IgnoreConfig = true;

                // Enable file based logging
                Vlc.DotNet.Core.VlcContext.StartupOptions.LogOptions.LogInFile = true;

                // Shows the VLC log console (in addition to the applications window)
                Vlc.DotNet.Core.VlcContext.StartupOptions.LogOptions.ShowLoggerConsole = true;

                // Set the log level for the VLC instance
                Vlc.DotNet.Core.VlcContext.StartupOptions.LogOptions.Verbosity = Vlc.DotNet.Core.VlcLogVerbosities.None;

                Vlc.DotNet.Core.VlcContext.StartupOptions.ScreenSaverEnabled = false;
                Vlc.DotNet.Core.VlcContext.StartupOptions.AddOption("--no-video-title");
                //hide played media filename on startingto play media.

                // Initialize the VlcContext
                Vlc.DotNet.Core.VlcContext.Initialize();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            int height = vlcControl.VideoProperties.Size.Height;
            int width = vlcControl.VideoProperties.Size.Width;        
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Close the VlcContext
            if (vlcControl.IsPlaying)
            {
                vlcControl.Stop();
            }
            
            Vlc.DotNet.Core.VlcContext.CloseAll();
            //Thread.Sleep(2000);
        }

        private void panelVideo_MouseWheelHandler(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                this.vlcControl.AudioProperties.Volume += 5;
                
            }

            if (e.Delta < 0)
            {
                this.vlcControl.AudioProperties.Volume -= 5;
            }

        }

        public void pauseVideo () {

            //MessageBox.Show(vlcControl.IsPlaying.ToString());
            if (vlcControl.IsPlaying)
            {
                vlcControl.Pause();
            }

        }
    }
}
